<template>
  <div class="app-container dialog-w80">
    <div class="customContainer">
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <el-date-picker v-model="pickerDate" value-format="yyyy.MM.dd" type="daterange" align="right" unlink-panels range-separator="-"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-select filterable placeholder="仓库" v-model="formInline.house_id">
            <el-option v-for="item in house_list" :key="item.house_id" :label="item.name" :value="item.house_id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="订单状态:" v-model="formInline.status">
            <el-option v-for="item in status" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="formInline.no" placeholder="请输入采购单号" @keyup.enter.native="searchClick"></el-input>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick">
            <template #default>搜索</template>
          </dj-button>
        </el-form-item>
      </el-form>

      <!-- 列表 -->
      <el-table highlight-current-row :data="list" v-loading.body="listLoading" border size="small">
        <el-table-column align="center" label='序号' width="50">
          <template slot-scope="scope">
            <span class="indexColor">{{scope.$index+1+(currentPage-1)*20}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="订单状态">
          <template slot-scope="scope">
            <span v-if="scope.row.status==0">待审核</span>
            <span v-if="scope.row.status==1">待确认</span>
            <span v-if="scope.row.status==2">待发货</span>
            <span v-if="scope.row.status==3">部分收货</span>
            <span v-if="scope.row.status==4">订单完结</span>
            <span v-if="scope.row.status==5">收货完成</span>
            <span v-if="scope.row.status==6">订单取消</span>
            <span v-if="scope.row.status==8">待收货</span>
            <span v-if="scope.row.status==9">申请退货中</span>
            <span v-if="scope.row.status==10">退货中</span>
            <span v-if="scope.row.status==11">退货完成</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="订单编号">
          <template slot-scope="scope">
            <el-button @click="get_order_info(scope.row.id,scope.$index,scope.row.status,scope.row.name,scope.row.order_id)" type="text" size="small">
              {{scope.row.order_id}}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="下单仓" prop="name"></el-table-column>
        <el-table-column align="center" label="下单时间" prop="creat_time"></el-table-column>
        <el-table-column align="center" label="备注">
          <template slot-scope="scope">
            <span class="indexColor">{{!scope.row.remark?'---':scope.row.remark}}</span>
          </template>
        </el-table-column>
        <!-- 列表的操作按钮 -->
        <el-table-column align="center" label="操作" width="100">
          <template slot-scope="scope">
            <el-button :disabled="scope.row.status!=0" @click="closeClick(scope.row.id,scope.row.status,scope.$index)" type="danger" size="mini">关闭订单
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹框 -->
    <el-dialog :close-on-click-modal="false" class="dialog_table" title="礼品部确认" :visible.sync="detail_dialog">
      <div class="dialogInContainer" v-loading="dialogLoading">
        <span style="position:absolute;top:20px;left:180px">订单编号：{{order_id}}</span>
        <div style="position:relative;top:-20px">下单仓：{{name}}</div>
        <el-table highlight-current-row :data="infolist" v-loading.body="listLoading" border size="small">
          <el-table-column align="center" label='序号' width="50">
            <template slot-scope="scope">
              <span class="indexColor">{{scope.$index+1}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="图片" width="70">
            <template slot-scope="scope">
              <img :src="scope.row.thumb" width="50" height="50" alt="">
            </template>
          </el-table-column>
          <el-table-column align="center" label="礼品名称" prop="gift_name"></el-table-column>
          <el-table-column align="center" label="礼品编号" prop="gift_id"></el-table-column>
          <el-table-column align="center" label="箱规" prop="box_num"></el-table-column>
          <el-table-column align="center" label="尺寸" prop="size"></el-table-column>
          <el-table-column align="center" label="三级分类" prop="type_name"></el-table-column>
          <el-table-column align="center" label="材质" prop="texture"></el-table-column>
          <el-table-column align="center" label="采购单价" prop="money_sell"></el-table-column>
          <el-table-column align="center" label="订单数">
            <template slot-scope="scope">
              <el-input onkeypress="return event.which>=48&&event.which<=57" type="number" onmousewheel="return false;" v-model="scope.row.total_num"
                placeholder="数量" @input="count"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" label="采购总价">
            <template slot-scope="scope">
              {{scope.row.total_num*1*scope.row.money_sell*1|keepTwoNum}}
            </template>
          </el-table-column>
          <el-table-column align="center" label="可采购数" prop="house_num"></el-table-column>
          <!-- 列表的操作按钮 -->
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-switch :disabled="isDisabled" active-value="0" inactive-value="2" v-model="scope.row.is_del" @change="count"></el-switch>
            </template>
          </el-table-column>
        </el-table>
        <!-- 自定义底部 -->
        <br>
        <div class="bottom_box">
          <el-button class="pull-right" :disabled="isDisabled" size="small" type="primary" @click="saveClick()">确认提交</el-button>
          <p class="pull-right" style="line-height:0">礼品总数量:{{totalOrder}} 　礼品总金额:{{totalPrice|keepTwoNum}} 　</p>
        </div>
      </div>
      <!-- </el-form> -->
    </el-dialog>
    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination class="pull-left" background @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="20"
        layout="prev, pager, next, jumper" :total="totalDataNumber">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { house, getList, getPage, order_info, save, close } from '@/api/goodsflow/gift_check';
import { pickerOptionsSixMonth, optionMaxTime198, pickerDateSixMonth } from '@/utils';
export default {
  data() {
    return {
      dialogLoading: false,
      listLoading: false,
      name: '',
      order_id: '',
      status: [
        { id: -1, name: '全部' },
        { id: 0, name: '待审核' },
        { id: 1, name: '待确认' },
        { id: 2, name: '待发货' },
        { id: 3, name: '部分收货' },
        { id: 4, name: '订单完结' },
        { id: 5, name: '收货完成' },
        { id: 6, name: '订单取消' },
        { id: 8, name: '待收货' },
        { id: 9, name: '申请退货中' },
        { id: 10, name: '退货中' },
        { id: 11, name: '退货完成' }
      ],
      formInline: {
        house_id: 0,
        no: '',
        start: '',
        end: '',
        status: 0
      },
      pickerOptions: pickerOptionsSixMonth(),
      pickerDate: pickerDateSixMonth(),
      house_list: [],
      list: null,
      totalDataNumber: 1,
      currentPage: 1,
      detail_dialog: false,
      infolist: [],
      switchVal: true,
      totalOrder: '0',
      totalPrice: '0',
      saveForm: {
        order_id: '',
        order_list: []
      },
      editIndex: '',
      isDisabled: false
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      //当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
      if (!vm.list) {
        vm.gethouse();
        vm.searchClick();
      }
    });
  },
  methods: {
    gethouse() {
      house().then(response => {
        this.house_list = response;
      });
    },
    searchClick() {
      optionMaxTime198(this.pickerDate).then(() => {
        //最大可选择时间区间
        this.formInline.start = this.pickerDate ? this.pickerDate[0] : '';
        this.formInline.end = this.pickerDate ? this.pickerDate[1] : '';
        this.fetchData();
      });
    },
    closeClick(id, status, index) {
      this.editIndex = index;
      this.$confirm('此操作为关闭, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        close({ order_id: id, status: status }).then(response => {
          this.$message({
            type: response.code == 1 ? 'success' : 'error',
            message: response.message
          });
          if (response.code == 1) {
            this.list[this.editIndex].status = 6;
          }
        });
      });
    },
    // 提交
    saveClick() {
      const that = this;
      let ok = true;
      that.saveForm.order_list = [];
      this.infolist.forEach(function(val, index) {
        if (val.total_num == '' || val.total_num <= 0) {
          return false;
        } else {
          that.saveForm.order_list.push({
            look_money: val.money_sell,
            gift_id: val.gift_id,
            total_num: val.total_num,
            is_del: val.is_del,
            remark: val.remark ? val.remark : ''
          });
        }
      });
      if (this.saveForm.order_list.length == this.infolist.length) {
        this.$confirm('此操作提交, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          save(this.saveForm).then(response => {
            this.$message({
              type: response.code == 1 ? 'success' : 'error',
              message: response.message
            });
            if (response.code == 1) {
              this.list[this.editIndex].status = 1;
              this.detail_dialog = false;
              this.searchClick();
            }
          });
        });
      } else {
        that.$message({ type: 'error', message: '请将所有信息填写完整!' });
        return;
      }
    },
    // 打开订单详情
    get_order_info(id, index, status, name, order_id) {
      this.dialogLoading = true;
      this.name = name;
      this.order_id = order_id;
      this.isDisabled = status == 0 ? false : true;
      this.editIndex = index;
      this.saveForm.order_id = id;
      this.detail_dialog = true;
      order_info(id).then(response => {
        this.infolist = response;
        this.count();
        this.dialogLoading = false;
      });
    },
    // 计算
    count() {
      var totalPrice = 0; //临时总价
      var totalOrder = 0;
      this.infolist.forEach((val, index) => {
        if (val.is_del * 1 == 0) {
          totalPrice += val.total_num * 1 * val.money_sell * 1; //累计总价
          totalOrder += val.total_num * 1;
        }
      });
      this.totalPrice = parseFloat(totalPrice);
      this.totalOrder = parseFloat(totalOrder);
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      getPage(this.formInline)
        .then(response => {
          this.totalDataNumber = response.page * 20;
          getList(this.formInline).then(response => {
            this.list = response;
            this.listLoading = false;
          });
        })
        .catch(res => {
          this.listLoading = false;
        });
    },
    handleCurrentChange(val) {
      this.listLoading = true;
      getList(this.formInline, val).then(response => {
        this.list = response;
        this.listLoading = false;
      });
    }
  }
};
</script>